﻿using WPFExcelReport.Controls;
using System.Collections;
using System.Collections.Generic;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using WPFExcelReport.Helpers;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class JLTAV37
    {
        double GDensity = 120;
        double GDays = 14;
        Dictionary<string, object> MachineControls;
        Dictionary<string, object> JobControls = new Dictionary<string, object>();
        public void SetupMachine()
        {
            GDensity = 120;
            MachineControls = Scheduler.DrawMachine(pagescope, SetEnv, machines, _syncContextTaskScheduler, GDays, GDensity);
        }
        private void btnSchedule_Click(object sender, RoutedEventArgs e)
        {
            AsyncRun("StartSchedule", machtyp, SetProd, SetEnv, SetData);
        }
        void vwBMKRF_CurrentChanged(object sender, EventArgs e)
        {
            if (dwBMKRF == null)
                return;
            try
            {
                KRS = dwBMKRF["ABFAUFKRS"].ToString();
                LFD = dwBMKRF["ABFAUFLFD"].ToString();
                POS = dwBMKRF["ABFAUFPOS"].ToString();

                if (FilterByMachineType)
                {
                    CreateMachineMatrix();
                }
                else
                {
                    btnSearch_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void CreateMachineMatrix()
        {
            string AMAGRBIS = "";
            Dictionary<string, object> ListMachines = new Dictionary<string, object>();
            Dictionary<string, object> ListJobs = new Dictionary<string, object>();
            Dictionary<string, object> _ListJobs = new Dictionary<string, object>();
            //GDensity = 480;
            var query = from DataRow row in SetEnv.Tables["AMSTAMM"].AsEnumerable()
                        where
                        row["AMNRKRS"].ToString() + row["AMLFDNR"].ToString() + row["AMPOS"].ToString()
                        == dwBMKRF["ABFBNRKRS"].ToString() + dwBMKRF["ABFBLFDNR"].ToString() + dwBMKRF["ABFBPOS"].ToString()
                        select row;
            foreach (DataRow row in query)
            {
                AMAGRBIS = row["AMAGRBIS"].ToString();
            }
            int count = ListMachineJob.Children.Count;
            ListMachineJob.Children.RemoveRange(0, count);
            query = from DataRow row in SetEnv.Tables["AMSTAMM"].AsEnumerable()
                    where
                    row["AMAGRBIS"].ToString() == AMAGRBIS
                    select row;
            foreach (DataRow row in query)
            {

                string machineid = Helper.GetMachineID(row["AMNRKRS"].ToString(), row["AMLFDNR"].ToString(), row["AMPOS"].ToString());
                string AMBEZ = row["AMBEZ"].ToString().Trim();
                string start = "";
                string end = "";
                Grid gm = null;
                Grid gd = (Grid)Helper.CreateMachineGrid("s", machineid, ref gm, AMBEZ, start, end, GDays, GDensity);
                ListMachines.Add((string)gd.Tag, gd);
                Task.Factory.StartNew(() => Scheduler.UpdateStack(pagescope, ListMachineJob, gd), new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler).Wait();
                Task.Factory.StartNew(() => Scheduler.UpdateMachineGrid(pagescope, gd, gm), new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler).Wait();
                string machine = row["AMNRKRS"].ToString() + row["AMLFDNR"].ToString() + row["AMPOS"].ToString();
                _ListJobs = Scheduler.ShowAllJob(SetData, SetEnv, ListMachines, pagescope, _syncContextTaskScheduler, GDensity, machine, "s", "s1");
                foreach (KeyValuePair<string, object> kp in _ListJobs)
                {
                    if (kp.Value != null)
                    {
                        ListJobs.Add(kp.Key, kp.Value);
                    }
                }
            }
            string jobid = dwBMKRF["ABFAUFKRS"].ToString() + "_" + dwBMKRF["ABFAUFLFD"].ToString() + "_" + dwBMKRF["ABFAUFPOS"].ToString();
            Grid job = null;
            if (ListJobs.ContainsKey("s1" + jobid))
            {
                job = (Grid)ListJobs["s1" + jobid];
            }
            if (job != null)
            {
                FocusJob(scheduler, ListMachineJob, job);
            }
        }
        public void FocusJob(AniScrollViewer viewer, StackPanel master, Grid job)
        {
            if (Lastjob != null)
            {
                UnFocusJob(Lastjob);
            }
            Lastjob = job;
            job.Background = Brushes.Wheat;
            DropShadowEffect effect = new DropShadowEffect();
            effect.BlurRadius = 10;
            effect.Color = Color.FromRgb(255, 255, 0);
            job.Effect = effect;
            RotateTransform rt = new RotateTransform();
            rt.Angle = -2;
            TransformGroup tg = new TransformGroup();
            tg.Children.Add(rt);
            job.RenderTransform = tg;
            Point relativePoint = job.TransformToAncestor(master).Transform(new Point(0, 0));
            ScrollToPosition(viewer, relativePoint.X, relativePoint.Y);
        }
        public void UnFocusJob(Grid job)
        {
            job.Background = Brushes.White;
            job.Effect = null;
            job.RenderTransform = null;
        }
        public void ScrollToPosition(AniScrollViewer viewer, double x, double y)
        {
            DoubleAnimation vertAnim = new DoubleAnimation();
            vertAnim.From = viewer.VerticalOffset;
            vertAnim.To = y;
            vertAnim.DecelerationRatio = .2;
            vertAnim.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
            DoubleAnimation horzAnim = new DoubleAnimation();
            horzAnim.From = viewer.HorizontalOffset;
            horzAnim.To = x;
            horzAnim.DecelerationRatio = .2;
            horzAnim.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
            Storyboard sb = new Storyboard();
            sb.Children.Add(vertAnim);
            sb.Children.Add(horzAnim);
            Storyboard.SetTarget(vertAnim, viewer);
            Storyboard.SetTargetProperty(vertAnim, new PropertyPath(AniScrollViewer.CurrentVerticalOffsetProperty));
            Storyboard.SetTarget(horzAnim, viewer);
            Storyboard.SetTargetProperty(horzAnim, new PropertyPath(AniScrollViewer.CurrentHorizontalOffsetProperty));
            sb.Begin();
        }
    }
}
